package com.peko.redis.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Peko
 */
@Controller
public class TestController {
    /**
     * 进入（获取session）
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("login")
    public void test(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //去redis获取session
        List<String> list = (List<String>)request.getSession().getAttribute("list");

        if(list == null){
            list = new ArrayList<>();
            request.getSession().setAttribute("list",list);
        }

        list.add("xxxx");
        //更新session到redis中（同步session）
        request.getSession().setAttribute("list",list);

        response.getWriter().println("size = "+list.size());
        response.getWriter().println("session_id = "+request.getSession().getId());

    }

    /**
     * 退出（删除session）
     * @param request
     * @param response
     */
    @RequestMapping("logout")
    public void logout(HttpServletRequest request, HttpServletResponse response){
        request.getSession().invalidate();
    }
}
